package xcmg.device.web;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.MaterialClassService;
import xcmg.device.service.vo.MaterialClassVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.Ref;
import yb.ecp.fast.infra.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿斯顿 on 2018/7/27.
 */
@RestController
@RequestMapping("/materialClass")
public class MaterialClassController extends BasicController {

    @Autowired
    private MaterialClassService materialClassService;

    @RequestMapping(value = "/inser",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.MaterialClass.ADD)
    public ActionResult insert(@RequestBody MaterialClassVO materialClassVO)throws Exception{
        materialClassVO.setCreateId(getUserId());
        materialClassVO.setUpdateId(getUserId());
        materialClassVO.setOrgId(getOrgId());
        if(materialClassVO.getMaterialCalssName().toCharArray().length>10){
            return actionResult(ErrorCode.MaterialsTypeTooLong);
        }
        ErrorCode code=materialClassService.insert(materialClassVO);
        return actionResult(code,materialClassVO.getId());
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.MaterialClass.EDIT)
    public ActionResult update(@RequestBody MaterialClassVO materialClassVO) throws Exception {
        if(materialClassVO.getId()==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        materialClassVO.setUpdateId(getUserId());
        materialClassVO.setOrgId(getOrgId());
        if(materialClassVO.getMaterialCalssName().toCharArray().length>10){
            return actionResult(ErrorCode.MaterialsTypeTooLong);
        }
        ErrorCode code=materialClassService.update(materialClassVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.MaterialClass.DELETE)
    public ActionResult delete(@RequestBody MaterialClassVO materialClassVO){
        if(materialClassVO.getId()==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        materialClassVO.setUpdateId(getUserId());
        Ref<List<String>> classNameList = new Ref<>(new ArrayList<>());
        ErrorCode code=materialClassService.delete(materialClassVO,classNameList);

        if(ErrorCode.ClassIsUsed==code)
        {
            return actionResult(code,classNameList.get());
        }
        return actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody MaterialClassVO materialClassVO)throws Exception{
        if(StringUtil.isNullOrEmpty(materialClassVO.getOrgId())){
            materialClassVO.setOrgId(getOrgId());
        }
        List<MaterialClassVO> result=materialClassService.list(materialClassVO);
        return actionValueResult(result);
    }

    @GetMapping(value = "/getAllSecondlist")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("获取所有二级列表")
    public ActionResult getAllSecondlist()throws Exception{
        List<MaterialClassVO> result=materialClassService.getAllSecondlist(getOrgId());
        return actionValueResult(result);
    }

    @GetMapping(value = "/getMaterialClassTreeList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("获取一级以及二级列表")
    public ActionResult getMaterialClassTreeList()throws Exception{
        List<MaterialClassVO> result=materialClassService.getMaterialClassTreeList(getOrgId());
        return actionValueResult(result);
    }

}
